<template>
  <view class="goods-list">
    <view v-for="(item,i ) in goodsList" :key="i" @click="gotoDetail(item)">
      <my_goods :goods="item"></my_goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数
        queryObj: {
          // 关键词
          query: '',
          // 分类ID
          cid: '',
          // 第几页
          pagenum: 1,
          // 每页的大小
          pagesize: 10
        },
        // 数据总条数
        total: 0,
        // 商品列表
        goodsList: [],
        // 节流阀
        isloding:false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isloding=true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloding=false
        if (res.meta.status !== 200) uni.$showMsg()
        this.total = res.message.total
        this.goodsList = [...this.goodsList,...res.message.goods]
        cb && cb()
        // console.log( this.total)
        // console.log( "dd"+this.goodsList)
      },
    // shuffleArray(array) {
    //   for (let i = array.length - 1; i > 0; i--) {
    //     // 生成 0 到 i 的随机数
    //     const j = Math.floor(Math.random() * (i + 1));
    //     // 交换元素
    //     [array[i], array[j]] = [array[j], array[i]];
    //   }
    //   return array;
    // }
    gotoDetail(item){
      // console.log(item)
      uni.navigateTo({
        url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
      })
    }
    },
    onReachBottom() {
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('数据加载完成')
      if(this.isloding) return 
    
      this.queryObj.pagenum =this.queryObj.pagenum + 1
      this.getGoodsList()
      console.log(this.queryObj.pagenum)
    },
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.total=0
      this.isloding=false
      this.goodsList=[]
      // console.log(this.queryObj.pagenum)
      // console.log(this.goodsList)
      this.getGoodsList(()=>uni.stopPullDownRefresh())
      // console.log('dxsx'+this.goodsList)
      // this.goodsList = [...this.shuffleArray(this.goodsList)]
      // console.log(this.goodsList)
    }
  }
</script>

<style lang="scss">
</style>